 cd "D:\Dropbox\Restat\"
log using Weisburd_Restat_Tables,replace

/************************************************************************************************************************

Program: Weisburd_Restat_Tables.do

Description: This program creates all tables in paper

Input File: weisburd_restat

                                                          
*************************************************************************************************************************/

clear 
clear matrix
set memory 500m

/*general summary*/

*Let's summarize things that don't change over time by client and
*things that change by policy!!!
eststo clear
use weisburd_restat,clear

  tab accident

  /*Table 1*/
  collapse (mean) owner_sex residence_accident distance d_* avg_inc bagrut engine year tot_premium car_value01 c_insurance /*
    */ winter accident (min) minyr=start_date (max) maxyr=end_date /*
    */ company (min) start_date (max) end_date period (count) cars=year , by (fid)
	
  gen full_period=(maxyr-minyr)/365.2
  gen start_year=year(start_date)
  gen end_year=year(end_date)
  
  estpost tabstat owner_sex full_period start_year end_year tot_premium car_value01 c_insurance /*
    */ residence_accident distance d_* avg_inc bagrut engine year, by(company) statistics(N mean sd) columns(statistics)

  esttab using table1.rtf, main(mean) aux(sd) star(* 0.10 ** 0.05 *** 0.01)/*
    */ replace label nogap onecell   unstack nonote nomtitle nonumber

  estpost ttest owner_sex full_period start_year end_year tot_premium car_value01 c_insurance /*
    */ residence_accident distance d_* avg_inc bagrut engine year, unequal by(company)

  esttab using table1_diff.rtf, se replace long nonumber nogap onecell mtitle("diff.") star(* 0.10 ** 0.05 *** 0.01)

use weisburd_restat,clear

  /*Table 2 - Accidents by period of coverage*/ 
  tabulate period company, summarize(d_accident)
  ttest d_accident if period==1, by(company)unequal
  ttest d_accident if period==2, by(company)unequal
  ttest d_accident if period==3, by(company)unequal
  ttest d_accident if period==4, by(company)unequal
  ttest d_accident if period==5, by(company)unequal
  ttest d_accident if period==6, by(company)unequal
  ttest d_accident if period==7, by(company)unequal
  ttest d_accident if period==8, by(company)unequal

  keep if period>1
  
  reg c_insurance company

  estpost tabstat d_accident accident c_insurance, /*
    */ by(company) statistics(N mean sd) columns(statistics) 

  esttab using table2_wald.rtf, main(mean) aux(sd) replace label nogap onecell   /*
    */ star(* 0.10 ** 0.05 *** 0.01) unstack nonote nomtitle nonumber

  estpost ttest d_accident accident c_insurance /*
    */ , by(company) unequal

  esttab using table2_wald_diff.rtf, se replace long nonumber nogap onecell mtitle("diff.") /*
   */ star(* 0.10 ** 0.05 *** 0.01)

*lets look at c_insurance in $100

use weisburd_restat,clear 
 
  drop if period==1  
  disp _N
  replace c_insurance=c_insurance/100
  tsset fid period 

  /*Table 3*/     
  
  eststo clear
    
  eststo: xtreg d_accident c_insurance car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */ policy_length p_winter, re cluster(fid) 
  
  eststo: ivreg2 d_accident (c_insurance=company), cluster(fid) bw() 
  
  eststo: ivreg2 d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter (c_insurance=company) , cluster(fid) bw() 

  eststo: ivreg2 d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1 (c_insurance=company) , cluster(fid) bw() 

  estout, cells(b se)
  esttab using table3.rtf, se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) replace label nogap onecell nonote nomtitle nonumber
  
  *run the analysis with alternative discount factors
  replace c_insurance_min=c_insurance_min/100
  
  ivreg2 d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1 (c_insurance_min=company) , cluster(fid) bw() 
  
  replace c_insurance_max=c_insurance_max/100
  
  ivreg2 d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1 (c_insurance_max=company) , cluster(fid) bw() 

  /*Table 4*/
  *nonlinear model

  eststo clear
  
  xtprobit d_accident c_insurance car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter,re
  
  mfx compute, predict(pu0) 
  est store m1
  
  ivprobit d_accident (c_insurance=company),vce(cluster fid)
  mfx, predict(p) 
  est store m2
  
  ivprobit d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter (c_insurance=company),vce(cluster fid)
  mfx, predict(p) 
  est store m3
  
  ivprobit d_accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1 (c_insurance=company),vce(cluster fid)
    
  mfx, predict(p)
  est store m4
  
  estout m1 m2 m3 m4, cells(b se) margin style(fixed)
  esttab using table4.rtf, se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)/*
    */ replace label nogap onecell nonote nomtitle nonumber
    
  *What if we look specifically at at-fault accidents
  /*Table 5*/
  
  eststo clear
    
  eststo: xtreg fault c_insurance car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */ policy_length p_winter, re cluster(fid) 
  
  eststo: ivreg2 fault (c_insurance=company), cluster(fid) bw() 
  
  eststo: ivreg2 fault car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter (c_insurance=company) , cluster(fid) bw() 

  eststo: ivreg2 fault car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1 (c_insurance=company) , cluster(fid) bw() 
   
  estout, cells(b se)
  esttab using table5.rtf, se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) replace label nogap onecell nonote nomtitle nonumber

  /*Table 6 - Multinomial Probit*/
  
  gen o_accident=0
  replace o_accident=1 if parking==1
  replace o_accident=2 if parking==0 & accident==1

  /*converges after 60 iterations*/

  cmp setup

  cmp (o_accident = c_insurance car_value01 engine year /*
    */  i.owner_sex distance bagrut avg_inc i.year_0* i.d_N* i.d_S* /*
    */   policy_length i.daccident1) (c_insurance=company car_value01 engine year /*
    */  i.owner_sex distance bagrut avg_inc i.year_0* i.d_N* i.d_S* /*
    */   policy_length i.daccident1), /*
    */ ind($cmp_mprobit $cmp_cont) nolr cluster(fid) tech(dfp nr) difficult
  
  *because the dummy variables all appear with an i. beforehand there margins will be computed
  *as the mean difference between norm(1)-norm(0) for each observation. 
  *we report the margin at the cutoff point of c_insurance=1.2 (the cost for company coverage)
  *For all other observationsit is computed at the mean  

  margins, dydx(*) force predict(equation(_outcome_1_2 ) pr) at (c_insurance=1.2) noatlegend
  margins, dydx(*) force predict(equation(_outcome_1_3 ) pr) at (c_insurance=1.2) noatlegend
 
/*Table 7 - DID*/ 
use weisburd_restat,clear
  xtset fid period
  gen post=0
  replace post=1 if period>1
  gen c_post=company*post
  replace new=0 if post>0
  bysort fid: egen is_new=sum(new)
  
  keep if is_new>=1
  
  tab company
  eststo clear
  
  *what if we did a simple difference in differences
  eststo: xtreg d_accident company post c_post car_value01 engine year mean_*/*
    */ owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */ p_winter policy_length, re cluster(fid) 
  
  replace post=0
  replace post=1 if period>2
  replace c_post=company*post

  eststo: xtreg d_accident company post c_post car_value01 engine year mean_*/*
    */ owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*     
	*/ p_winter policy_length if period>1, re cluster(fid) 

  replace post=0
  replace post=1 if period>3
  replace c_post=company*post

  eststo: xtreg d_accident company post c_post car_value01 engine year mean_*/*
    */ owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */ p_winter policy_length if period>2, re cluster(fid) 
   
  replace post=0
  replace post=1 if period>4
  replace c_post=company*post

  eststo: xtreg d_accident company post c_post car_value01 engine year mean_*/*
    */ owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */ p_winter policy_length if period>3, re cluster(fid) 
 
  estout, cells(b se)
  esttab using table7.rtf, se b(3) se(3) star(* 0.10 ** 0.05 *** 0.01)/*
*/ replace label nogap onecell nonote nomtitle nonumber

/*Robustness Check - Accidents as count variable*/   
use weisburd_restat,clear  
  
  drop if period==1  
  disp _N  
  summarize c_insurance
  replace c_insurance=c_insurance/100
  
  *lagged accident is clearly endogenous and correlated with c_insurance so it cannot be included in the regression
  tsset fid period 
  
    ivpois accident car_value01 engine year /*
    */  owner_sex distance bagrut avg_inc year_0* d_N* d_S* /*
    */   policy_length p_winter mean_* daccident1, endog(c_insurance) exog(company) 
    
    *a $100 increase in accident costs results in 10% less accidents
 

  
log close
